package com.ouc.yyi.config.shiro;

import com.ouc.yyi.config.model.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

//@Controller
public class TestController {
    @RequestMapping("/login")
    public String login(){
       return "login.html";
    }
    @RequestMapping("/index")
    public String index(){
        return "index.html";
    }
    @RequestMapping("/loginUser")
    public String loginUser(@RequestParam("username")String username,
            @RequestParam("password") String password,HttpSession session){
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        try{
            //进行登陆
            subject.login(usernamePasswordToken);
            //登陆成功后获得到用户
            User user = (User)subject.getPrincipal();
            //放到session中去,在授权中获得
            session.setAttribute("user",user);
            return "index.html";
        }catch (Exception e){
            e.printStackTrace();
            return "login.html";
        }
    }
    @RequestMapping("/loginout")
    public String loginout(){
        Subject subject = SecurityUtils.getSubject();
        if (subject!=null){
            subject.logout();
        }
        return "login.html";
    }
    @RequestMapping("/admin")
    @ResponseBody
    public String logins(){
        return "login success";
    }
}
